package com.jgp.security.service;

import com.jgp.common.pojo.TreeBean;
import com.jgp.common.utils.InsertUpdateDelete;
import com.jgp.common.utils.JGPUtil;
import com.jgp.security.secmodel.SecRole;
import com.jgp.security.secmodel.SecRoleUser;
import com.jgp.security.secmodel.SecUser;
import com.jgp.security.secrepository.SecRoleRepository;
import com.jgp.security.secrepository.SecRoleUserRepository;
import com.jgp.security.secrepository.SecUserRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-11-02
 */
@Service
@Transactional(transactionManager = "secondTransactionManager",readOnly = true)
public class SecRoleUserService {
    @Lazy
    @Autowired
    private SecRoleUserRepository repository;
    @Lazy
    @Autowired
    private SecRoleRepository roleRepository;
    @Lazy
    @Autowired
    private SecUserRepository userRepository;
    
    public List<Long> queryRoleIds(String userId) {
        if (StringUtils.isBlank(userId)) return null;
        List<SecRoleUser> roleUsers = repository.findByUserId(userId);
        List<Long> ids = roleUsers.stream().map(SecRoleUser::getRoleId).collect(Collectors.toList());
        return ids;
    }
    
    public List<SecRole> queryRoles(String userId){
        List<Long> roleIds = queryRoleIds(userId);
        return roleRepository.read(roleIds);
    }
    
    public List<String> queryUserIds(Long roleId){
        List<SecRoleUser> roleUsers = repository.findByRoleId(roleId);
        List<String> ids = roleUsers.stream().map(SecRoleUser::getUserId).collect(Collectors.toList());
        return ids;
    }
    
    public List<SecUser> queryUsers(Long roleId){
        List<String> userIds = queryUserIds(roleId);
        return userRepository.read(userIds);
    }
    
    /**
     * 查询指定人所有角色
     *
     * @param userId
     * @return 返回指定人的角色树，不存在无关觉色
     */
    public List<TreeBean> queryPrivateRoleTree(String userId) {
        List<Long> ids = queryRoleIds(userId);
        if (Objects.isNull(ids)) return new ArrayList<>();
        return roleRepository.queryTreeFromBottomToTop(ids);
    }
    
    /**
     * 修改用户角色
     *
     * @param userId
     * @param roleIds
     */
    @Transactional(transactionManager = "secondTransactionManager")
    public void modifyUserRole(String userId, List<Long> roleIds) {
        List<SecRoleUser> roleUsers = repository.findByUserId(userId);
        List<Long> persistRoleIds = roleUsers.stream().map(SecRoleUser::getRoleId).collect(Collectors.toList());
        InsertUpdateDelete<Long> insertUpdateDelete = JGPUtil.iud(roleIds, persistRoleIds);
        
        if (insertUpdateDelete.getDeletes().size() > 0) {
            repository.deleteByUserIdAndRoleIdIn(userId, insertUpdateDelete.getDeletes());
        }
        
        if (insertUpdateDelete.getInserts().size() > 0) {
            List<SecRoleUser> newItems = new ArrayList<>();
            for (Long roleId : insertUpdateDelete.getInserts()) {
                SecRoleUser rm = new SecRoleUser();
                rm.setUserId(userId);
                rm.setRoleId(roleId);
                newItems.add(rm);
            }
            repository.createInBatch(newItems);
        }
    }
    
    public List<String> queryUserIds(List<Long> roleIds) {
        List<SecRoleUser> roleUsers = repository.findByRoleIdIn(roleIds);
        return roleUsers.stream().map(SecRoleUser::getUserId).collect(Collectors.toList());
    }
}
